package com.yj.consumer.controller;

import com.yj.consumer.pojo.Pet;
import com.yj.consumer.pojo.User;
import com.yj.consumer.service.ProviderService;
import com.yj.consumer.util.I18nUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import sun.nio.cs.ThreadLocalCoders;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.net.URI;
import java.security.Provider;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ThreadLocalRandom;

@RestController
@Slf4j
public class ConsumerController {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @Resource
    ProviderService providerService;

    @Autowired
    I18nUtil i18nUtil;

    @GetMapping("/index")
    public String index(){
        List<ServiceInstance> instanceList = discoveryClient.getInstances("provider");
        int index = ThreadLocalRandom.current().nextInt(instanceList.size());
        ServiceInstance serviceInstance = instanceList.get(index);
        int port = serviceInstance.getPort();
        URI uri = serviceInstance.getUri();
        log.info("提供方的端口为：{}",port);

        return "访问到的Uri为"+uri+" 端口为："+port;
    }

    @GetMapping("/index2")
    public String index2(){
        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://provider/index", String.class);

        String body = responseEntity.getBody();

        log.info("收到的内容:{}",body);
        return body;

    }

    @GetMapping("/index3")
    public String getProvider(){
        String port = providerService.index2();
        log.info("访问到的端口号：{}",port);
        return port;
    }

    @GetMapping("/setPet")
    public Pet setPet(@RequestParam("pet") Pet petInfo){
        System.out.println(petInfo);
        return petInfo;
    }


    @GetMapping("/i18n")
    public String i18n(String info){
//        ResourceBundle welcome = ResourceBundle.getBundle("static.message", LocaleContextHolder.getLocale(new LocaleContext() {
//            @Override
//            public Locale getLocale() {
//                return Locale.US;
//            }
//        }));
//        System.out.println(welcome.getString("welcome"));
        System.out.println(info);
        String key = i18nUtil.getMessage(info);
        System.out.println(key);
        return key;
    }

    @GetMapping("/user")
    public String getUser(@Valid User user){
        if (StringUtils.isEmpty(user.getName())){
            return i18nUtil.getMessage("validator.not.null",new Object[]{"用户名"});
        }
        if (StringUtils.isEmpty(user.getAddress())){
            return i18nUtil.getMessage("validator.not.null",new Object[]{"地址"});
        }
        return "ok";
    }
}
